-- times to iterate the process
maxgen = 50

function cell(x, y)
 -- check a cell state
 if get_value(x, y) > 0.5 then
   return 1
 else
   return 0
 end
end

function living(x, y)
 -- check if a cell will live
 -- depending on his 8 neighbors
 v = cell(x - 1, y - 1) +
     cell(x, y - 1) +
     cell(x + 1, y - 1) +
     cell(x - 1, y) +
     cell(x + 1, y) +
     cell(x - 1, y + 1) +
     cell(x, y + 1) +
     cell(x + 1, y + 1)
 if v < 2 then
   -- will die for loneliness
   return -1
 elseif v > 3 then
   -- will die for crowding
   return -1
 elseif v == 3 then
   -- new cell come to life!
   return 1
 else
   -- nothing change
   return 0
 end
end

totcycles = maxgen * (bound_y1 - bound_y0)
cycle = 0
for g = 1, maxgen do
 for y = bound_y0, bound_y1 do
   for x = bound_x0, bound_x1 do
     v = living(x, y)
     if v == 1 then
        set_value(x, y, 1)
     elseif v == -1 then
        set_value(x, y, 0)
     end
   end
   cycle = cycle + 1
   progress(cycle / totcycles)
 end
 flush()
 Dog_Refresh()
end
